﻿// -----------------------------------------------------------------------
// <copyright file="LegalGroundsListData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db LegalGroundsList
    /// </summary>
    public class LegalGroundsListData
    {
        /// <summary>
        /// Get LegalGroundsList By Id
        /// </summary>
        /// <param name="legalGroundsListId">Param legalGroundsListId</param>
        /// <returns>Return LegalGroundsList</returns>
        public LegalGroundsListEN GetLegalGroundsListById(int legalGroundsListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                LegalGroundsListEN legalGroundsList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneLegalGroundsListGetById, legalGroundsListId);
                while (reader.Read())
                {
                    legalGroundsList = new LegalGroundsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        legalGroundsList.LegalGroundsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        legalGroundsList.Name = reader.GetString(1);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return legalGroundsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all LegalGroundsList
        /// </summary>
        /// <returns>Return list all LegalGroundsList</returns>
        public IList<LegalGroundsListEN> GetLegalGroundsLists()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<LegalGroundsListEN> lstLegalGroundsList = new List<LegalGroundsListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneLegalGroundsListGetAll);
                while (reader.Read())
                {
                    LegalGroundsListEN legalGroundsList = new LegalGroundsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        legalGroundsList.LegalGroundsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        legalGroundsList.Name = reader.GetString(1);
                    }

                    lstLegalGroundsList.Add(legalGroundsList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstLegalGroundsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db LegalGroundsList
        /// </summary>
        /// <param name="legalGroundsList">Paarm LegalGroundsList</param>
        /// <returns>Insert LegalGroundsList</returns>
        public int Insert(LegalGroundsListEN legalGroundsList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.LegalGroundsList.Name, legalGroundsList.Name)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneLegalGroundsListInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db LegalGroundsList
        /// </summary>
        /// <param name="legalGroundsList">Param LegalGroundsList</param>
        public void Update(LegalGroundsListEN legalGroundsList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.LegalGroundsList.LegalGroundsListId, legalGroundsList.LegalGroundsListId),
                                    new SqlParameter(Constants.SqlParamerterName.LegalGroundsList.Name, legalGroundsList.Name)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneLegalGroundsListUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete LegalGroundsList by legalGroundsListId
        /// </summary>
        /// <param name="legalGroundsListId">param legalGroundsListId</param>
        public void Delete(int legalGroundsListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.LegalGroundsList.LegalGroundsListId, legalGroundsListId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneLegalGroundsListDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist name
        /// </summary>
        /// <param name="legalGroundsListId">Param legalGroundsListId</param>
        /// <param name="name">Param name</param>
        /// <returns>Return true or false</returns>
        public bool Exist(int legalGroundsListId, string name)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.LegalGroundsList.LegalGroundsListId, legalGroundsListId),
                    new SqlParameter(Constants.SqlParamerterName.LegalGroundsList.Name, name)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneLegalGroundsListExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all LegalGroundsList By Name
        /// </summary>
        /// <param name="profileTypeId">param profileTypeId</param>
        /// <param name="name">param name</param>
        /// <returns>Return list all LegalGroundsList</returns>
        public IList<LegalGroundsListEN> GetLegalGroundsListSearch(int profileTypeId, string name)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<LegalGroundsListEN> lstLegalGroundsList = new List<LegalGroundsListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneLegalGroundsListSearch, profileTypeId, !string.IsNullOrWhiteSpace(name) ? name.Trim() : (object)DBNull.Value);
                while (reader.Read())
                {
                    LegalGroundsListEN legalGroundsList = new LegalGroundsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        legalGroundsList.LegalGroundsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        legalGroundsList.Name = reader.GetString(1);
                    }

                    lstLegalGroundsList.Add(legalGroundsList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstLegalGroundsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        public LegalGroundsListEN GetLegalGroundsListByName(string name)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                LegalGroundsListEN legalGroundsList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneLegalGroundsListByName, name);
                while (reader.Read())
                {
                    legalGroundsList = new LegalGroundsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        legalGroundsList.LegalGroundsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        legalGroundsList.Name = reader.GetString(1);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return legalGroundsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all LegalGroundsList By profileTypeId
        /// </summary>
        /// <param name="profileTypeId">param profileTypeId</param>
        /// <param name="name">param name</param>
        /// <returns>Return list all LegalGroundsList</returns>
        public IList<LegalGroundsListEN> GetLegalGroundsListByProfileTypeId(int profileTypeId, string name)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<LegalGroundsListEN> lstLegalGroundsList = new List<LegalGroundsListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneLegalGroundsListGetByProfileTypeId, profileTypeId, !string.IsNullOrWhiteSpace(name) ? name.Trim() : (object)DBNull.Value);
                while (reader.Read())
                {
                    LegalGroundsListEN legalGroundsList = new LegalGroundsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        legalGroundsList.LegalGroundsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        legalGroundsList.Name = reader.GetString(1);
                    }

                    lstLegalGroundsList.Add(legalGroundsList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstLegalGroundsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}